package com.iss.ress;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iss.utils.IOHelper;
import com.iss.utils.Parameter;

public final class ResourcePool extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private boolean isDebug = false;
    private String jsPackage = null;
    private String cssPackage = null;
    
    private String newLine = null;
    
    private Hashtable<String,String> jsTable = new Hashtable<String,String>(); 
    private Hashtable<String,String> cssTable = new Hashtable<String,String>(); 
    public ResourcePool() {
        super();
    }
    
    public void init(){
    	isDebug = Parameter.getNullSafeValue(getInitParameter("debug"),false);
    	jsPackage = getInitParameter("jsPackage");
    	cssPackage = getInitParameter("cssPackage");
    	newLine = System.getProperty("line.separator");
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String script = request.getParameter("js");
		String cssP = request.getParameter("css");
		if (script!= null){
			StringBuffer buffer = new StringBuffer();
			String[] jsRes = script.split(",");
			for(String js:jsRes){
				try{
					String content = getJS(js+".js");
					buffer.append(content);
					buffer.append(newLine);
				}catch(Exception e){
					if (isDebug) throw new RuntimeException("Can't find '"+js+".js' .");
				}
			}
			response.getWriter().write(buffer.toString());
			
		}else if(cssP!=null){
			
			StringBuffer buffer = new StringBuffer();
			String[] cssRes = cssP.split(",");
			for(String css:cssRes){
				try{
					String content = getCSS(css+".css");
					buffer.append(content);
					buffer.append(newLine);
				}catch(Exception e){
					if (isDebug) throw new RuntimeException("Can't find '"+css+".css' .");
				}
			}
			response.getWriter().write(buffer.toString());
		}
	}
	
	private String getResource(String pack,String name) throws IOException {
		if (pack==null) throw new RuntimeException("package is null");
		String path = getServletContext().getRealPath(pack);
		File f = new File(path, name);
		FileInputStream fis = new FileInputStream(f);
		String content = IOHelper.Stream2String(fis);
		return content;
	}
	
	private String getJS(String name) throws IOException{
		if (isDebug) return getResource(jsPackage,name);
		if (!jsTable.containsKey(name)){
			String str = getResource(jsPackage,name);
			jsTable.put(name, str);
			return str;
		}
		return jsTable.get(name);
	}
	
	private String getCSS(String name) throws IOException{
		if (isDebug) return getResource(cssPackage,name);
		if (!cssTable.containsKey(name)){
			String str = getResource(cssPackage,name);
			cssTable.put(name, str);
			return str;
		}
		return cssTable.get(name);
	}
}
